const app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eIndex: 0,
    popupBoolean: false,
    specifiBoolean: false,
    selectGoodss:[],
    goodsImage:'',
    goodsName:'',
    goodsSurname:'',
    goodsColor:'',
    isNotBuy:false
  },

  // 菜单切换类名on
  tab: function (e) {
       var sceneId = e.currentTarget.dataset.index
       this.setData({
            sceneId: sceneId
       })
       this.loadScene()
  },
   radioChange: function (e){
        var all=e.detail.value
        var allArray = all.split("_")
        //将选择的内容放入数组中
        var thisGoods = [allArray[0], allArray[1], allArray[2], allArray[3], allArray[4]]
        //加入到总数组中  如果有 则替换
        var selectGoodss = this.data.selectGoodss
        if (selectGoodss.length>0){
             var isReplace=0;
             var index;
             for (var i = 0; i < selectGoodss.length; i++) {
                  var oldGoods = selectGoodss[i]
                  if (oldGoods[0] == thisGoods[0]) {
                       isReplace=1
                       index=i
                       break;
                  }
             }
             //如果需要替换  则替换
             if(isReplace==1){
                  selectGoodss.splice(index,1,thisGoods)  
             //不需替换  则增加     
             }else{
                  selectGoodss.push(thisGoods)   
             }

             //如果总数组中没有数据 则直接添加
        }else{
             selectGoodss.push(thisGoods)
        }  

         this.setData({
              selectGoodss: selectGoodss
         }) 
       
  },

  // 点击坐标
  markClick: function (e) {
    this.setData({
      popupBoolean: true,
         goodsImage: e.currentTarget.dataset.value.goodsImg,
         goodsName: e.currentTarget.dataset.value.goodsName,
         goodsSurname: e.currentTarget.dataset.value.goodsSurname,
         goodsColor: e.currentTarget.dataset.value.goodsColor
    })
  },

  // 隐藏坐标弹出层
  maskClick: function () {
    this.setData({
      popupBoolean: false,
      specifiBoolean: false
    })
  },

  // 加入购物车
  choose1: function () {
    this.setData({
         cartType:1,
      specifiBoolean: true,
         isNotBuy: false
    })
  },
  //立即购买
     choose2: function () {
          this.setData({
               cartType: 2,
               specifiBoolean: true,
               isNotBuy: false
          })
     },

     //送朋友
     choose3: function () {
          this.setData({
               cartType: 3,
               specifiBoolean: true,
               isNotBuy: false
          })
     },

  // 确认
     confirmCart: function (e) {
    // let id = e.currentTarget.id
      
       let that=this
       var cartType=this.data.cartType
       var selectGoodss = this.data.selectGoodss
       var goodsDtos = this.data.thisScene.goodsDtos
       //1，判断是否有哪个商品没有选择尺码
       if(selectGoodss.length<=0){
            wx.showToast({
                 title: '请选择尺码',
                 icon: 'none',
                 duration: 1500
            })
            return  
       }
       
       for (var i = 0; i < goodsDtos.length;i++){
            var goods=goodsDtos[i]
            var isSelect = 0;
            for(var j=0;j<selectGoodss.length;j++){
                 var selectGoods = selectGoodss[j]
                 if (selectGoods[0]==goods.id){
                      isSelect=1    
                 }   
            }
            if(isSelect==0){
                 wx.showToast({
                      title: '请选择' + goods.goodsName+'尺码',
                      icon: 'none',
                      duration: 1500
                 })
                 return  
            }
       }

       //2，组装接口参数
       var goodsIds = '';
       var goodsCodeIds = '';
       var goodsPrices='';
       var goodsNums='';
          console.log(selectGoodss)
       for (var j = 0; j < selectGoodss.length; j++) {
            var selectGoods = selectGoodss[j]
            if (selectGoods[1] == 'null' || selectGoods[2]=='null'){
                 wx.showToast({
                      title: '购买失败',
                      icon: 'none',
                      duration: 1500
                 })
                 return  
            }
            goodsIds+=selectGoods[1]+','
            goodsCodeIds += selectGoods[2] + ','
            goodsPrices += selectGoods[3] + ','
            goodsNums += selectGoods[4] + ','
       }
       goodsIds = goodsIds.substring(0, goodsIds.length-1)
       goodsCodeIds = goodsCodeIds.substring(0, goodsCodeIds.length - 1)
       goodsPrices = goodsPrices.substring(0, goodsPrices.length - 1)
       goodsNums = goodsNums.substring(0, goodsNums.length - 1)
       //设置不能点击
          this.setData({
               isNotBuy: true
          })  
       //3，请求接口
       var data = { 'memberId': app.globalData.userInfo.id, 'sceneId': this.data.thisScene.id, 'cartType': cartType, 'num':1,'goodsPrices': goodsPrices, 'goodsNums': goodsNums, 'goodsIds': goodsIds, 'goodsCodeIds': goodsCodeIds}
       var promise = app.synPostData('cart/addCart', data)
       promise.then(function (res) {
            if (res.data.code != 0) {
                 wx.showToast({
                      title: '加入购物车失败',
                      icon: 'none',
                      duration: 1500
                 })
            } else {
                 if(cartType==2){
                      wx.navigateTo({
                           url: '../confir-order/confir-order?cartIds=' + res.data.result+'&cartType='+cartType
                      })     
                 } else if (cartType == 3){
                      var data1 = { 'memberId': app.globalData.userInfo.id, 'cartIds': res.data.result}
                      var promise = app.synPostData('order/create', data1)
                      promise.then(function (res1) {
                           if (res.data.code != 0) {
                                wx.showToast({
                                     title: '创建订单失败',
                                     icon: 'none',
                                     duration: 1500
                                })
                           }else{
                                //开启预支付
                                wx.navigateTo({
                                     url: '../payment/payment?orderIds=' + res1.data.result.orderIds + '&payAmount=' + res1.data.result.payAmount + '&cartType=' + res1.data.result.cartType
                                }) 
                           }  
                      })
                 } else {
                      wx.showToast({
                           title: '加入购物车成功',
                           icon: 'none',
                           duration: 1500
                      })
                      that.setData({
                           popupBoolean: false,
                           specifiBoolean: false
                      })
                 }
                 
            }
       });  

  },
  toCart:function(){
       wx.switchTab({
            url: '../cart/cart'
       })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (param) {
     this.setData({
          sceneId: param.sceneId
     })
     this.loadScene()
     this.loadScenes()
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  loadScene:function(){
       let that = this
       var data = {'sceneId': this.data.sceneId }
       var promise = app.synPostData('scene/findOne', data)
       promise.then(function (res) {
            that.setData({
                 thisScene: res.data.result,
                 selectGoodss: []
                 
            })
       }); 
  },
  loadScenes:function(){
       let that = this
       var promise = app.synPostData('scene/select', '')
       promise.then(function (res) {
            that.setData({
                 scenes: res.data.result
            })
       }); 
  },
  setNoCheck:function(){
      
  }
})